var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 0,
    menuIndex: 0,
    menuList: [
      {
        icon: '/pkgBak/imgs/home-m1.png',
        name: '点击退押金',
      },
      {
        icon: '/pkgBak/imgs/home-m2.png',
        name: '客服',
      },
      {
        icon: '/pkgBak/imgs/home-m3.png',
        name: '查询归还位置',
      },
      {
        icon: '/pkgBak/imgs/home-m4.png',
        name: '管理平台',
      },
      {
        icon: '/pkgBak/imgs/home-m5.png',         //菜单图标
        name: '租金统计',
      },
      {
        icon: '/pkgBak/imgs/home-m6.png',         //菜单图标
        name: '帮带借',
      }
    ],
    menuPageSize: 5, // 每页菜单数量
    menuTotalPage: 0, // 菜单总分页数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const vm = this

    //计算菜单分页数
    vm.setData({
      menuTotalPage: Math.ceil(vm.data.menuList.length / vm.data.menuPageSize)
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    const vm = this
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  clickTab(e) {
    const vm = this
    vm.setData({
      tabIndex: parseInt(e.currentTarget.dataset.index)
    })
  },

  clickMenu(e) {
    const vm = this
    let index = parseInt(e.currentTarget.dataset.index)
    if(index == 0) {
      this.toRefundDeposit()
      return
    }
    if(index == 1) {
       this.contacted()
       return
    }
    if(index == 2) {
      this.toLocation()
      return
    }
    if(index == 3) {

    }
    if(index == 4) {
       this.lease_record()
       return
    }
    wx.showToast({
      icon: 'none',
      title: vm.data.menuList[index].name,
    })
  },

  onSwiperChange(e) {
    const vm = this
    vm.setData({
      menuIndex: e.detail.current
    })
  },

  mine_center: function () {
    wx.navigateTo({
      url: '../user_center/user_center',
    })
  },

    /**
   * 跳转到关注公众号的页面
   */
  goFollowMp: function () {
    app.goFollowMp(-200001, '关注公众号-首页')
  },

  /**
   * 去退押金
   */
  toRefundDeposit: function () {
    let that = this;
    var nophone = app.getUserLoginNoPhone();
    if (nophone == null || nophone == undefined || nophone == 1) {
      that.setData({
        phoneAuthStatus: true,
        phoneAuthBtt: 'deposit_record',
        loading_show: false
      });
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return
    }
    wx.navigateTo({
      url: '../user_record/deposit/user_deposit',
    })
  },
  /**
   * 在线客服
   */ 
  contacted() {
    wx.navigateTo({
      url: '../callCenter/callCenter',
    })
  },

  toLocation: function () {
    var qrc = '';
    if (this.data.qrc) {
      qrc = this.data.qrc;
    }
    wx.navigateTo({
      url: '../../../pkgBak/pages/help/locations/locations?qrc=' + encodeURIComponent(qrc),
    })
  },

  lease_record:function(){
    wx.navigateTo({
      url: '../user_record/lease/user_lease',
    })
  },

  getRightBtns: function () {
    var that = this;
    that.data.isFirstJudge = false;
    app.executeRequest(function (header) {
      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/time_card_patient/care_show', //判断是否有未归还
        header: header,
        success: function (res) {
          let netErr = app.handleRes(res, that);
          if (netErr) {
            return
          }

          // 获取是否关注公众号变量
          if (res.data.isSubOffical != undefined && (res.data.isSubOffical == false || res.data.isSubOffical == true)) {
            app.globalData.isSubOffical = res.data.isSubOffical;
            console.log('是否关注公众号：' + app.globalData.isSubOffical);
          } else {
            app.globalData.isSubOffical = false;
            console.log('是否关注公众号：未下发变量');
          }
          // 记录展示关注公众号日志
          if (!app.globalData.isSubOffical) {
            app.recordShowMpLog(-200001, '关注公众号-首页')
          }

          var careShowFlg = res.data.data
          var manageShowFlg = res.data.authManageFlg
          var statisticCount = res.data.statisticVisit;
          var optAdminCount = res.data.adminVisit;
          let powerAdminFlg = res.data.powerAdminFlg;


          that.setData({
            careShowStat: careShowFlg,
            managerShowStat: manageShowFlg,
            statisticBtnFlg: statisticCount > 0,
            optAdminBtnFlg: optAdminCount > 0,
            powerAdminFlg: powerAdminFlg > 0,
            isSubOffical: app.globalData.isSubOffical,
          })

          //是否添加或去除管理按钮
          that.getShowOutpatientIcon(careShowFlg, manageShowFlg, that.data.controls)

          if (res.data.noBack && !that.data.isScanQrc) {
            wx.setStorageSync("user_no_back", res);
            that.setData({
              connectType: '归还'
            })
            //未归还的
            if (that.data.pt != 'noback') { //200114 冯慧敏修改 在免押前就设置了openning_show为true，因此此处去掉openning_show的判断
              noDepositLease.getLeaseNoDepositInfo(app.globalData.appOnShowData, that);
            }

            if (that.data.openning_show) {
              that.setData({
                openning_show: false
              })
            }

          } else if (res.data.noPay && !that.data.isScanQrc) {
            wx.setStorageSync("user_no_pay", res);
            //未支付
            if (that.data.pt != 'noreturnequ') {
              noDepositLease.getLeaseNoDepositInfo(app.globalData.appOnShowData, that);
            } else if (that.data.openning_show) {
              that.setData({
                openning_show: false
              })
            }
          } else {
            console.log("--remove-careshow-noback")
            wx.removeStorageSync('user_no_back');
            that.setData({
              connectType: '租借',
              openning_desc: '【home】正在查询支付/授权结果-282...'
            })
            that.recordBlueConnectError(ConstantData.err_type.deposit_query, '正在查询支付/授权结果...');
            wx.removeStorageSync('user_no_pay');
            noDepositLease.getLeaseNoDepositInfo(app.globalData.appOnShowData, that);
          }
        },
        fail: function (res) {
          if (that.data.openning_show) {
            that.setData({
              openning_show: false
            })
          }
          app.handleRes(res, that);
        }
      })
    })

  },

  

})